package com.ww.springboot.boot.baseTest.design.代理;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-02-04 16:34
 */
public class DynamicProxyCglib implements MethodInterceptor {

    private Enhancer enhancer = new Enhancer();

    public <T> T getProyInfo(Class<T> suoperClass) {
        return (T) enhancer.create(suoperClass, this);
    }


    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.err.println("-------调用方法之前---------------");
        //是invokeSuper 不是invoke
        Object invokeSuper = proxy.invokeSuper(o,args);
        System.out.println(invokeSuper);

        //不能使用method.invoke 不然会一直递归
//        Object invoke = method.invoke(o, args);
//        System.out.println(invoke);
        System.err.println("-------调用方法之后---------------");
        return invokeSuper;
    }
}
